package cn.tedu.bonfire.admin.account.controller;

import cn.tedu.bonfire.admin.account.pojo.dto.*;
import cn.tedu.bonfire.admin.account.pojo.vo.UserManagementVO;
import cn.tedu.bonfire.admin.account.service.IUserAndRoleService;

import cn.tedu.bonfire.admin.account.service.IUserService;
import cn.tedu.bonfire.common.pojo.vo.PageData;
import cn.tedu.bonfire.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
@Validated
@Slf4j
@Api(tags="1.用户管理")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IUserAndRoleService userAndRoleService;


    /**
     * 添加用户
     * @param userAddNewDTO
     * @return
     */
    //@AdminAccountAddUser(action = "添加用户")
    @PostMapping("/add-new")
//    @PreAuthorize("hasAuthority('/account/user/add-new')")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Validated UserAddNewDTO userAddNewDTO){
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    /**
     * 删除用户
     */
    //@AdminAccountDeleteUser(action = "删除用户")
    @PostMapping("/delete")
    @ApiOperation("删除用户")
//    @PreAuthorize("hasAuthority('/account/user/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户Id",
                    paramType = "query", dataType = "Long")
    })
    public JsonResult deleteUser(@Range(min = 1, message = "请提交合法的用户ID值！")Long id){
        userService.deleteByUserId(id,2);
        return JsonResult.ok();
    }

    /**
     * 修改用户昵称
     * @param userNicknameUpdateDTO
     * @return
     */
    //@AdminAccountUpdateNickName(action = "修改用户昵称")
    @PostMapping("/update/nickname")
    @ApiOperation("修改用户昵称")
//    @PreAuthorize("hasAuthority('/account/user/edit')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickname", value = "昵称",
                    paramType = "query", dataType = "String")
    })
    public JsonResult updateUserInformation(UserNicknameUpdateDTO userNicknameUpdateDTO){
        userService.updateNickname(userNicknameUpdateDTO.getUserId(), userNicknameUpdateDTO.getNickname());
        return  JsonResult.ok();
    }


    /**
     * 修改头像
     * @param userAvatarDTO
     * @return
     */
    //@AdminAccountUpdateAvatar(action = "修改用户头像")
    @PostMapping("/update/avatar")
    @ApiOperation("修改头像")
//    @PreAuthorize("hasAuthority('/account/user/edit')")
    @ApiOperationSupport(order = 310)
    public JsonResult updateAvatarInformation(UserAvatarDTO userAvatarDTO){
        userService.updateAvatar(userAvatarDTO.getUserId(), userAvatarDTO.getAvatar());
        return  JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
//    @PreAuthorize("hasAuthority('/account/user/enable')")
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id) {
        userService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
//    @PreAuthorize("hasAuthority('/account/user/enable')")
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 321)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id) {
        userService.setDisable(id);
        return JsonResult.ok();
    }



    /**
     * 分配角色
     * @param userUpdateRoleNameDTO
     * @return
     */
    //@AdminAccountRoleUser(action = "分配角色")
    @PostMapping("/update/role")
    @ApiOperationSupport(order = 340)
//    @PreAuthorize("hasAuthority('/account/user/edit')")
    @ApiOperation("分配角色")
    public JsonResult updateRoleName(UserUpdateRoleNameDTO userUpdateRoleNameDTO){
        userAndRoleService.updateUserRoleById(userUpdateRoleNameDTO.getUserId(),
                userUpdateRoleNameDTO.getRoleId());
        return JsonResult.ok();
    }


    /**
     * 分页查询用户
     * @param page
     * @return
     */
    @GetMapping("")
    @ApiOperation("分页查询用户")
    @ApiOperationSupport(order = 420)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1",
//                    paramType = "query", dataType = "int")
//    })
    public JsonResult getStandardById(@Range(min = 1, message = "请提交有效的页码值！")Integer page) {

        Integer pageNum = page == null ? 1 : page;
        PageData<UserManagementVO> pageData = userService.list(pageNum);
        return JsonResult.ok(pageData);
    }


    /**
     * ES搜索查询预热
     * @return
     */



}
